//
//  SMRightContentViewCtrl.swift
//  SmartMachine
//
//  Created by Sherman's iMac on 2019/11/6.
//  Copyright © 2019 Smart. All rights reserved.
//

import Cocoa

class SMRightContentViewCtrl: NSViewController, NSTableViewDelegate, NSTableViewDataSource {

    var outputScrollvTextView : NSScrollView!
    var tableView : NSTableView!
    
    lazy var sourceArray : NSMutableArray = {
        
        let arr = NSMutableArray.init()
        let path : String! =  Bundle.main.path(forResource: "ConfigTableColum", ofType: "plist")
        let tempArr : NSArray? = NSArray.init(contentsOfFile: path)
        for (_, dict) in tempArr!.enumerated() {
           var modal = SMColumnModal.init(dict: dict as! NSDictionary)
           arr.add(modal)
        }
        return arr
    }()
    
    override func loadView() {
        
        let contentView = NSView.init()
        contentView.wantsLayer = true
        //contentView.layer?.backgroundColor = FUNC_COLOR(r: 35, g: 47, b: 98).cgColor
        self.view = contentView
    }
    
    func configViews() {
        
        outputScrollvTextView = NSTextView.scrollableTextView()
        outputScrollvTextView.backgroundColor = NSColor.clear
        
        let textView : NSTextView? = outputScrollvTextView.documentView as? NSTextView;
        if textView != nil {
            textView?.backgroundColor = NSColor.clear
            
            textView?.font = NSFont.systemFont(ofSize: 18.0)
            textView?.textColor = NSColor.white
            textView?.string = "这是日志输出。。。"
        }
        
        self.view.addSubview(outputScrollvTextView)
        
        tableView = NSTableView.init(frame: NSZeroRect)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.focusRingType = NSFocusRingType.none
        tableView.selectionHighlightStyle = NSTableView.SelectionHighlightStyle.none
        tableView.gridColor = NSColor.lightGray
//        tableView.gridStyleMask = NSTableView.GridLineStyle.solidHorizontalGridLineMask
        tableView.backgroundColor = NSColor.clear
        
        for (_, modal) in sourceArray.enumerated() {
            
            let smModal : SMColumnModal! = modal as? SMColumnModal
            
            let idStr : String = smModal.name ?? "";
            let col : NSTableColumn! = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: idStr))
            
            col!.title = idStr
            col.width = 100
            tableView.addTableColumn(col)
        }
        
        let sourceScrollV = NSScrollView.init(frame: NSZeroRect)
        sourceScrollV.hasVerticalScroller = false
        sourceScrollV.drawsBackground = false
        sourceScrollV.documentView = tableView
        self.view.addSubview(sourceScrollV);
        
        outputScrollvTextView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(100)
        }
        
        sourceScrollV.snp.makeConstraints { (make) in
            
            make.left.right.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-120)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
         
        configViews()
    }
     
    func numberOfRows(in tableView: NSTableView) -> Int {
        return 5
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        
        let rowView : SMSourceTableRowView! = SMSourceTableRowView.init(frame: NSZeroRect)
        rowView.titleLab.stringValue = String.init(format: "%ld", row)
        return rowView
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        return 50.0
    }
}
